<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        if ($request->expectsJson()) {
            return null;
        }

        // 保存当前完整URL（包括查询参数）作为意图访问的URL
        session()->put('url.intended', $request->fullUrl());

        // 如果是微信服务账号相关路径，重定向到微信登录页
        if (str_starts_with($request->path(), 'wechat-service-account')) {
            return route('wechat.service.account.login');
        }

        return route('login');
    }
}